package cn.iocoder.yudao.module.worker.controller.app.trendlike;

import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.worker.controller.app.trendlike.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.trendlike.TrendLikeDO;
import cn.iocoder.yudao.module.worker.service.trendlike.TrendLikeService;

@Tag(name = "用户 APP - 动态点赞")
@RestController
@RequestMapping("/worker/trend-like")
@Validated
public class AppTrendLikeController {

    @Resource
    private TrendLikeService trendLikeService;

    @PostMapping("/create")
    @Operation(summary = "创建动态点赞")
    @PreAuthenticated
    public CommonResult<Long> createTrendLike(@Valid @RequestBody AppTrendLikeSaveReqVO createReqVO) {
        createReqVO.setUserId(getLoginUserId());
        return success(trendLikeService.createTrendLike(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新动态点赞")
    public CommonResult<Boolean> updateTrendLike(@Valid @RequestBody AppTrendLikeSaveReqVO updateReqVO) {
        trendLikeService.updateTrendLike(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除动态点赞")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteTrendLike(@RequestParam("id") Long id) {
        trendLikeService.deleteTrendLike(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得动态点赞")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppTrendLikeRespVO> getTrendLike(@RequestParam("id") Long id) {
        TrendLikeDO trendLike = trendLikeService.getTrendLike(id);
        return success(BeanUtils.toBean(trendLike, AppTrendLikeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得动态点赞分页")
    public CommonResult<PageResult<AppTrendLikeRespVO>> getTrendLikePage(@Valid AppTrendLikePageReqVO pageReqVO) {
        PageResult<TrendLikeDO> pageResult = trendLikeService.getTrendLikePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppTrendLikeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出动态点赞 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTrendLikeExcel(@Valid AppTrendLikePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TrendLikeDO> list = trendLikeService.getTrendLikePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "动态点赞.xls", "数据", AppTrendLikeRespVO.class,
                        BeanUtils.toBean(list, AppTrendLikeRespVO.class));
    }

}